package cn.iocoder.yudao.module.design.controller.app;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.design.controller.admin.problem.vo.*;
import cn.iocoder.yudao.module.design.convert.problem.ProblemConvert;
import cn.iocoder.yudao.module.design.dal.dataobject.problem.ProblemDO;
import cn.iocoder.yudao.module.design.service.problem.ProblemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 问题")
@RestController
@RequestMapping("/design/problem")
@Validated
public class AppProblemController {

    @Resource
    private ProblemService problemService;

    @PostMapping("/create")
    @Operation(summary ="创建问题")
    @PreAuthorize("@ss.hasPermission('design:problem:create')")
    public CommonResult<Long> createProblem(@Valid @RequestBody ProblemCreateReqVO createReqVO) {
        return success(problemService.createProblem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary ="更新问题")
    @PreAuthorize("@ss.hasPermission('design:problem:update')")
    public CommonResult<Boolean> updateProblem(@Valid @RequestBody ProblemUpdateReqVO updateReqVO) {
        problemService.updateProblem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary ="删除问题")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('design:problem:delete')")
    public CommonResult<Boolean> deleteProblem(@RequestParam("id") Long id) {
        problemService.deleteProblem(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary ="获得问题")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('design:problem:query')")
    public CommonResult<ProblemRespVO> getProblem(@RequestParam("id") Long id) {
        ProblemDO problem = problemService.getProblem(id);
        return success(ProblemConvert.INSTANCE.convert(problem));
    }

    @GetMapping("/list")
    @Operation(summary ="获得问题列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('design:problem:query')")
    public CommonResult<List<ProblemRespVO>> getProblemList(@RequestParam("ids") Collection<Long> ids) {
        List<ProblemDO> list = problemService.getProblemList(ids);
        return success(ProblemConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary ="获得问题分页")
    @PreAuthorize("@ss.hasPermission('design:problem:query')")
    public CommonResult<PageResult<ProblemRespVO>> getProblemPage(@Valid ProblemPageReqVO pageVO) {
        PageResult<ProblemDO> pageResult = problemService.getProblemPage(pageVO);
        return success(ProblemConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary ="导出问题 Excel")
    @PreAuthorize("@ss.hasPermission('design:problem:export')")
   
    public void exportProblemExcel(@Valid ProblemExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ProblemDO> list = problemService.getProblemList(exportReqVO);
        // 导出 Excel
        List<ProblemExcelVO> datas = ProblemConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "问题.xls", "数据", ProblemExcelVO.class, datas);
    }

}
